var listAdd="", numAdd = 1;

function usernameRe(){						//username register
	var re = 0;
	var username= $("#username").val(), tmp;
	if( $("#username").val().length <6 ) {
			$("#errorusername").text("username length is least than 6 character");
			$("#errorusername").show("blind", { direction: "horizontal" }, "fast");
			$("#errorusername").removeClass('ok');
			$("#errorusername").addClass('error');
			return 0;
		} else {
			
			var url = "/befriend/index/check";
			$.ajax({
				url: url,
				type: 'POST',
				data: { username : username },
				success: function( data, status ){
					tmp = data;
					if( (tmp == 0) || ( tmp == "0") ){
						$("#errorusername").text('ok');
						$("#errorusername").show();
						$("#errorusername").removeClass('error');
						$("#errorusername").addClass('ok');
						re = 1;
						
					} else {
						$("#errorusername").text('username have used!');
						
					}
				},
				error: function( data, status ){
					alert( 'error' );
					
				},
				complete: function(data, status){
					return re;
				}
			});		
	}
	return re;
}

function passRe(){							//passRe 
	$("#errorpass").show();
	if( $("#pass").val().length <6 ) {
			$("#errorpass").text("password length is least than 6 character").show("blind", { direction: "horizontal" }, "fast").addClass("error");
			return 0;
		} else {
			$("#errorpass").text('');
			$("#errorpass").hide();
			return 1;
	}	
}

function fullname(){							//fullname 
	$("#errorfullname").show();
	if( $("#fullname").val().length <6 ) {
			$("#errorfullname").text("fullname length is least than 6 character").show("blind", { direction: "horizontal" }, "fast").addClass("error");
			return 0;
		} else {
			$("#errorfullname").text('');
			$("#errorfullname").hide();
			return 1;
	}	
}

function email(){							//email 
	$("#erroremail").show();
	if( $("#email").val().length <6 ) {
			$("#erroremail").text("email length is least than 6 character").show("blind", { direction: "horizontal" }, "fast").addClass("error");
			return 0;
		} else {
			$("#erroremail").text('');
			$("#erroremail").hide();
			return 1;
	}	
}



function repassRe(){							//repass 
	$("#errorrepass").show();
	if( $("#pass").val().length <6){
		$("#errorpass").text("password length is least than 6 character").show("blind", { direction: "horizontal" }, "fast").addClass("error");
		return 0;
	}else {
		if( $("#repass").val() != $("#pass").val() ) {
			$("#errorrepass").text("password and repassword not match").show("blind", { direction: "horizontal" }, "fast").addClass("error");
			return 0;
		} else {
			$("#errorrepass").hide();
			$("#errorrepass").text('');
			return 1;
		}	
	}
}

function address(){							//address 
	$("#erroraddress").show();
	if( $("#address").val().length ==0 ) {
			$("#erroraddress").text("input your address").show("blind", { direction: "horizontal" }, "fast").addClass("error");
			return 0;
		} else {
			$("#erroraddress").text('');
			$("#erroraddress").hide();
			return 1;
	}	
}



function dob(){
	var dateStr =$("#month").val() + "-" + $("#day").val() + "-" + $("#year").val();
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray ==null) {
		$("#errordob").text("Please enter date day-month-year").show("blind", { direction: "horizontal" }, "fast").addClass("error");
		return false;
	}

	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];
	$("#errordob").show();
	if (month < 1 || month > 12) { // check month range
		$("#errordob").text("Month must be between 1 and 12.").show("blind", { direction: "horizontal" }, "fast").addClass("error");
		return false;
	}

	if (day < 1 || day > 31) {
		$("#errordob").text("Day must be between 1 and 31.").show("blind", { direction: "horizontal" }, "fast").addClass("error");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		$("#errordob").text("Month "+month+" doesn`t have 31 days!");
		return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			$("#errordob").text("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
	$("#errordob").hide();
	$("#errordob").text("");
	
	return true; // date is valid
}


function address(  ) {
	var address = $("#address").val();
	var url = "/asdf/index/address";
	$.ajax({
		url: url,
		type: 'POST',
		data: { address : address },
		success: function( data, status ){
			if( data != null ) {
				$("#addSelect").show();
				listAdd = data.split("-");
				listAddress( 1 );
			} else {
				$("#addSelect").hide();
			}
		},
		error: function( data, status ){
			
		}
	});

}

function listAddress( num1 ) {
	numAdd = num1;
	var num = 1 +  Math.round( (num1-1 ) / 5 );
	var i, end = 5*num;
	var tmp = "";
	if( end > listAdd.length-1 ) end = listAdd.length-1;
	for( i = 1*num; i<= listAdd.length-1; i++ ){
		if( i ==num1 ){
			tmp =tmp + "<div class='add' onClick='addClick();' onMouseOver='addHover("+ i +" );'>"+listAdd[ i ]+"</div>";
		} else {
			tmp =tmp + "<div class='add' onClick='addClick(); ' onMouseOver='addHover( "+ i +"  );'>"+listAdd[ i ]+"</div>";
		}
		
	}
	$("#addSelect").html( tmp );
}

function addClick(){
	$("#addSelect").hide();
}
function addHover( i){

	$(document).ready(function(){
		$("#address").val(listAdd[ i ]);
	});
}


//music

var  musicAll="";
function music(  ) {
	
	var music = $("#music").val();
	var url = "/ad/index/music";
	$.ajax({
		url: url,
		type: 'POST',
		data: { music : music },
		success: function( data, status ){
			
			if( data != null ) {
				$("#musSelect").show();
				listMus = data.split("-");
				listMusic(  );
			} else {
				$("#musSelect").hide();
			}
		},
		error: function( data, status ){
			
		}
	});

}

function listMusic(  ) {

	var i;
	var tmp = "";
	
	for( i = 1; i<= listMus.length-1; i++ ){
		if( checkMus( listMus[ i ])){
			if( i ==1 ){
				tmp =tmp + "<div class='mus' onClick='musClick("+i+"  );' >"+listMus[ i ]+"</div>";
			} else {
				tmp =tmp + "<div class='mus' onClick='musClick("+i+"  );' >"+listMus[ i ]+"</div>";
			}
		} 
		
	}
	
	$("#musSelect").html( tmp );
	if( tmp=="") $("#musSelect").hide();
}

function musClick( i ){
	
	if( musicAll != "" ) musicAll = musicAll  +"-" +listMus[ i ] ;
	else  musicAll = listMus[ i ] ;
	showMusic();
	
}


function checkMus( name ) {
	var tmp = musicAll.split("-");
	for( var i = 0; i< tmp.length ; i++ ) {
		if( name == tmp[ i ] ){
			return false;
		}
	}
	return true;
}
function showMusic () {
	var mus  = musicAll.split("-");
	var tmp="";
	for( var i = 0 ; i< mus.length ; i++ ){
		if( mus[ i ] != "" ) tmp = tmp + "<span class='token'>" + mus[ i ] +"<img class='xoa' onClick='xoamusic("+i+");' src='/images/xoa2.png' ></img> </span>" ;
	}
	$("#musiccell").html(tmp);
	$("#music").val("");
	$("#musSelect").hide();
}

function xoamusic( j ){
	$("#musiccell").text("");
	var tmp="";
	var mus  = musicAll.split('-');
	musicAll = "";
	for( var i = 0 ; i< mus.length ; i++ ){
		
		if( ( mus[ i ] != "" ) && ( j !=  i ) ) {
			tmp = tmp + "<span class='token'>" + mus[ i ] +"<img class='xoa' onClick='xoamusic("+i+");' src='/images/xoa2.png' ></img> </span>" ;
			if( musicAll == "" )  musicAll = mus[ i ] ;
			else musicAll = musicAll + "-" +  mus[ i ];
		}
		
	}
	$("#musiccell").html(tmp);
	$("#musSelect").hide();
}


//sport

var  sportAll="";
function sport(  ) {
	
	var sport = $("#sport").val();
	var url = "/ad/index/sport";
	$.ajax({
		url: url,
		type: 'POST',
		data: { sport : sport },
		success: function( data, status ){
			
			if( data != null ) {
		
				$("#spoSelect").show();
				listSpo = data.split("-");
				
				listSport(  );
			} else {
				$("#spoSelect").hide();
			}
		},
		error: function( data, status ){
			
		}
	});

}

function listSport(  ) {
	
	var i;
	var tmp = "";
	
	for( i = 1; i<= listSpo.length-1; i++ ){
		if( checkSpo( listSpo[ i ] ) ) {
			if( ( i ==1 ) ){
				tmp =tmp + "<div class='spo' onClick='spoClick("+i+"  );' >"+listSpo[ i ]+"</div>";
			} else {
				tmp =tmp + "<div class='spo' onClick='spoClick("+i+"  );' >"+listSpo[ i ]+"</div>";
			}
		} 
		
	}
	$("#spoSelect").show();
	$("#spoSelect").html( tmp );
	if( tmp=="") $("#spoSelect").hide();
}

function checkSpo( name ) {
	var tmp = sportAll.split("-");
	for( var i = 0; i< tmp.length ; i++ ) {
		if( name == tmp[ i ] ){
			return false;
		}
	}
	return true;
}

function spoClick( i ){
	
	if( sportAll != "" ) sportAll = sportAll  +"-" +listSpo[ i ] ;
	else  sportAll = listSpo[ i ] ;
	showSport();
	
}

function showSport () {
	
	var spo  = sportAll.split("-");
	var tmp="";
	for( var i = 0 ; i< spo.length ; i++ ){
		if( spo[ i ] != "" ) tmp = tmp + "<span class='token'>" + spo[ i ] +"<img class='xoa' onClick='xoasport("+i+");' src='/images/xoa2.png' ></img> </span>" ;
	}
	$("#sportcell").html(tmp);
	$("#sport").val("");
	$("#spoSelect").hide();
}

function xoasport( j ){
	$("#sportcell").text("");
	var tmp="";
	var spo  = sportAll.split('-');
	sportAll = "";
	for( var i = 0 ; i< spo.length ; i++ ){
		
		if( ( spo[ i ] != "" ) && ( j !=  i ) ) {
			tmp = tmp + "<span class='token'>" + spo[ i ] +"<img class='xoa' onClick='xoasport("+i+");' src='/images/xoa2.png' ></img> </span>" ;
			if( sportAll == "" )  sportAll = spo[ i ] ;
			else sportAll = sportAll + "-" +  spo[ i ];
		}
		
	}
	$("#sportcell").html(tmp);
	$("#spoSelect").hide();
}

function checkInfo(){
	if(!( ($("#errorusername").text()== "ok")
			&& passRe()
			&& repassRe()
			&& dob()
			&& fullname()
			&& email()
			&& sportAll.length
			&& musicAll.length 
	)){
		
		$("#button").show();
		$("#submit").hide();
		$("#check").text("you haven't input enough infomation yet!");
	}else {
		$("#music").val( musicAll );
		$("#sport").val( sportAll );
		$("#dangki").submit();
	}
}
$(document).ready(function(){
	$("#submit").hide();
	$("#addSelect").hide();
	$("#musSelect").hide();
	$("#spoSelect").hide();
	$("#address").blur(function(){
		$("#addSelect").hide();
	});
	
	$("#username").blur( function(){		//username register
		usernameRe();
	});
	
	$("#pass").blur( function(){		//pass register
		passRe();
	});
	
	$("#repass").focus( function(){		//repass register
		repassRe();
	});
	$("#repass").blur( function(){		//repass register
		repassRe();
	});
	$("#fullname").blur( function(){		//repass register
		fullname();
	});
	
	$("#email").blur( function(){		//repass register
		email();
	});

	$("#day").blur( function(){		//dob register
		dob();
	});
	$("#month").blur( function(){		//dob register
		dob();
	});
	$("#year").blur( function(){		//dob register
		dob();
	});
	
	$("#address").keyup( function(){		//address register
		address();
	});
	
	$("#music").keyup( function(){		//music register
		music();
	});
	$("#sport").keyup( function(){		//music register
		sport();
	});
	$("#button").click( function(){		//music register
		checkInfo();
	});
	
	$("#reset").click( function(){
		$("#username").val("");
		$("#pass").val("");
		$("#repass").val("");
		$("#email").val("");
		$("#fullname").val("");
		$("#address").val("");
		$(".dob").val("");
		$("#music").val("");
		$("#sport").val("");
		$("#avartar").val("");
		$("#male").attr("checked", "checked");
		
		$("#errorusername").text("").hide();
		$("#errorpass").text("").hide();
		$("#errorfullname").text("").hide();
		$("#errorrepass").text("").hide();
		$("#erroremail").text("").hide();
		$("#erroraddress").text("").hide();
		$("#errordob").text("").hide();
		sportAll = "";
		musicAll = "";
		$(".token").html("").hide();
		
	});
	
});









